//
//  MainUITabBarController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/8/26.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "MainUITabBarController.h"
#import "Main1ViewController.h"
#import "UIColor+Hex.h"
#import "AppDelegate.h"
#import "UMMobClick/MobClick.h"
@interface MainUITabBarController ()
@property NSMutableArray *btns;
@end

@implementation MainUITabBarController

- (void)viewDidLoad {
    
    //AppDelegate *myDelegate = [UIApplication sharedApplication];
   // myDelegate.tabcv = self;
    
    
    NSLog(@"MainUITabBarController");
    [super viewDidLoad];
    //self.tabBar.hidden = YES;
    //#ff7e00
   // self.tabBar.tintColor=[UIColor colorWithHexString:@"ff7e00"];
   
    

    [self setBottomView1];
    
    
  //  [self initCompleteBlock];
  //  [self configLocationManager];
    
    
    
    
}
-(void)setBottomView1{
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];

    NSMutableArray *items = [[NSMutableArray alloc]init];
    
    
    UINavigationController *main1 = [storyboard instantiateViewControllerWithIdentifier:@"item1"];
    UINavigationController *main2 = [storyboard instantiateViewControllerWithIdentifier:@"item2"];
    UINavigationController *main3 = [storyboard instantiateViewControllerWithIdentifier:@"item3"];
    UINavigationController *main4 = [storyboard instantiateViewControllerWithIdentifier:@"item4"];
    UINavigationController *main5 = [storyboard instantiateViewControllerWithIdentifier:@"item5"];
    
    [items addObject:main1];
    [items addObject:main2];
    [items addObject:main3];
    [items addObject:main4];
    [items addObject:main5];
    
    
    CGRect rect = self.tabBar.frame;
    
    
  //  self.tabBar.hidden = YES;
    
    UIView *myview = [[UIView alloc]init];
    //myview.frame = rect;
    
    myview.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
    myview.backgroundColor = [UIColor whiteColor];
    self.tabView = myview;
    
    NSArray *defImg = [[NSArray alloc]initWithObjects:@"home_index_default.png",@"home_order_default.png",@"home_schedule_default.png",@"home_grab_order_default",@"home_people_default", nil];
    
    NSArray *selImg = [[NSArray alloc]initWithObjects:@"home_index_pressed.png",@"home_order_pressed.png",@"home_schedule_pressed.png",@"home_grab_order_pressed.png",@"home_people_pressed.png", nil];
    
    NSLog(@"height==%f",myview.frame.size.height);
    self.btns = [[NSMutableArray alloc]init];
    for (int i = 0; i < selImg.count; i++) {
        
        UIImage *img = [UIImage imageNamed:[defImg objectAtIndex:i]];
        UIImage *selectimg = [UIImage imageNamed:[selImg objectAtIndex:i]];
        
        CGFloat x = i * myview.frame.size.width / selImg.count;
        
        CGRect iRect = CGRectMake(x, 0, myview.frame.size.width / selImg.count, myview.frame.size.height);
        
        UIButton *button = [[UIButton alloc]initWithFrame:iRect];
        
        button.frame = iRect;
        
        
        
        //[button setBackgroundColor:[UIColor blueColor]];
        [button setImage:img forState:UIControlStateNormal];
        [button setImage:selectimg forState:UIControlStateSelected];
        //button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        button.layer.masksToBounds = YES;
        
        if (i == 0) {
            //[button setBackgroundImage:selectimg forState:UIControlStateNormal];
            button.selected = YES;
            self.selectButton = button;
        }
        
        [button setTag:i];
        [self.btns addObject:button];
        //[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [myview addSubview:button];
        
    };
    
    
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, myview.frame.size.width, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"#f1f1f1" alpha:80];
    line.layer.masksToBounds = YES;
    
    [myview addSubview:line];
    [self.tabBar addSubview:myview];
    // self.tabBar.tintColor = [UIColor colorWithRed:255.0/255.0 green:124.0/255.0 blue:56.0/255.0 alpha:1];
    
    
    
    self.viewControllers = items;
}
//
//-(void)setBottomView2{
//    
//    
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
//
//    NSMutableArray *items = [[NSMutableArray alloc]init];
//    
//        Main1ViewController *main1 = [storyboard instantiateViewControllerWithIdentifier:@"item1"];
//    
//        main1.tabBarItem.image = [UIImage imageNamed:@"home_index_default"];
//        main1.tabBarItem.selectedImage = [UIImage imageNamed:@"home_index_pressed"];
//    
//    
//    
//        [items addObject:main1];
//    
//        Main1ViewController *main2 = [storyboard instantiateViewControllerWithIdentifier:@"item2"];
//    
//        main2.tabBarItem.image = [UIImage imageNamed:@"home_order_default"];
//        main2.tabBarItem.selectedImage = [UIImage imageNamed:@"home_order_pressed"];
//        [items addObject:main2];
//    
//        Main1ViewController *main3 = [storyboard instantiateViewControllerWithIdentifier:@"item3"];
//    
//    
//        main3.tabBarItem.image = [UIImage imageNamed:@"home_schedule_default"];
//        main3.tabBarItem.selectedImage = [UIImage imageNamed:@"home_schedule_pressed"];
//    
//        [items addObject:main3];
//    
//        Main1ViewController *main4 = [storyboard instantiateViewControllerWithIdentifier:@"item4"];
//    
//        main4.tabBarItem.image = [UIImage imageNamed:@"home_grab_order_default"];
//        main4.tabBarItem.selectedImage = [UIImage imageNamed:@"home_grab_order_pressed"];
//    
//        [items addObject:main4];
//    
//        Main1ViewController *main5 = [storyboard instantiateViewControllerWithIdentifier:@"item5"];
//    
//        main5.tabBarItem.image = [UIImage imageNamed:@"home_people_default"];
//        main5.tabBarItem.selectedImage = [UIImage imageNamed:@"home_people_pressed"];
//        
//        [items addObject:main5];
//
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)btnClick : (UIButton *)button{
    
    
    self.selectButton.selected  = NO;
    
    button.selected = YES;
    self.selectButton = button;
    
    
    self.selectedIndex = button.tag;
    
}

- (void)showIndex:(int) index{
    
    UIButton *btn = self.btns[index];
    self.selectButton.selected  = NO;
    
    btn.selected = YES;
    self.selectButton = btn;
    
    
    self.selectedIndex = index;
    
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSLog(@"didSelectItem");
    if (self.selectButton.tag != item.tag - 1) {
        UIButton * button = self.btns[item.tag - 1];
        
        self.selectButton.selected  = NO;
        
        button.selected = YES;
        self.selectButton = button;
        
        
        self.selectedIndex = button.tag;
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)initCompleteBlock
{
    __weak MainUITabBarController *weakSelf = self;
    self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
    {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            //如果为定位失败的error，则不进行annotation的添加
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
        
        //得到定位信息，添加annotation
        if (location)
        {
            //_lat = location.coordinate.latitude;
            //_lon = location.coordinate.longitude;
            NSLog(@"%f",location.coordinate.latitude);
            NSLog(@"%f",location.coordinate.longitude);
        }
    };
}

- (void)configLocationManager
{
    self.locationManager = [[AMapLocationManager alloc] init];
    
    [self.locationManager setDelegate:self];
    
    //设置期望定位精度
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
    //设置不允许系统暂停定位
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
    //设置允许在后台定位
    [self.locationManager setAllowsBackgroundLocationUpdates:YES];
    
    //设置定位超时时间
    //[self.locationManager setLocationTimeout:DefaultLocationTimeout];
   
    //设置逆地理超时时间
    //[self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
    //进行单次定位请求
    [self.locationManager setLocatingWithReGeocode:YES];
    [self.locationManager startUpdatingLocation];
}
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
    //得到定位信息，添加annotation
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);

    if (location)
    {
        //_lat = location.coordinate.latitude;
        //_lon = location.coordinate.longitude;
       // NSLog(@"%f",location.coordinate.latitude);
       // NSLog(@"%f",location.coordinate.longitude);
       // NSLog(@"%@",reGeocode.formattedAddress);
    }
}

#pragma mark - AppDelegate methods

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    [MobClick beginLogPageView:@"OnePage"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"OnePage"];
}

@end
